﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

public partial class BalanceCenter_AdminUserEdit : PageBase
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.IsPostBack)
        {
            dtUserBirthday.Value = "1970-01-01";

            FillProvince();

            FillddlBank();

            long id = -1;
            id = ShoveEL._Convert.StrToLong(this.Request["id"], -1);
            if (id < 0)
            {
                btnSave.Enabled = false;
                btnSave.Visible = false;

                btnNetEdit.Enabled = false;
                btnNetEdit.Visible = false;

                btnUserNet.Enabled = false;
                btnUserNet.Visible = false;
                btnUserScore.Enabled = false;
                btnUserScore.Visible = false;
                btnUserScore2.Enabled = false;
                btnUserScore2.Visible = false;
            }
            else
            {
                tbID.Text = id.ToString();

                BindData(id);
            }
        }
    }

    #region Web 窗体设计器生成的代码

    override protected void OnInit(EventArgs e)
    {
        isRequestLogin = true;
        RequestCompetences = Competences.BuildCompetencesList(Competences.MemberManagement);
        RequestLoginPage = "BalanceCenter/AdminUserEdit.aspx";

        base.OnInit(e);
    }

    #endregion

    private void FillProvince()
    {
        DataTable dt = DAL.Tables.T_Province.Select("", "", "[id]");

        if (dt == null)
        {
            PublicFunction.GoError(ErrorNumber.DataReadWrite, "数据读写错误", this.GetType().BaseType.FullName);

            return;
        }

        ShoveEL.ControlExt.FillDropDownList(ddlUserProvince, dt, "Name", "ID");        

        if (ddlUserProvince.Items.Count > 0)
        {
            ddlUserProvince.SelectedIndex = 0;

            FillCity(int.Parse(ddlUserProvince.SelectedValue));
        }
    }

    private void FillCity(int ProvinceID)
    {
        DataTable dt = DAL.Tables.T_City.Select("", "ProvinceID = " + ProvinceID.ToString(), "[id]");

        if (dt == null)
        {
            PublicFunction.GoError(ErrorNumber.DataReadWrite, "数据读写错误", this.GetType().BaseType.FullName);

            return;
        }

        ShoveEL.ControlExt.FillDropDownList(ddlUserCity, dt, "Name", "ID");

        if (ddlUserCity.Items.Count > 0)
        {
            ddlUserCity.SelectedIndex = 0;
        }
    }

    private void FillddlBank()
    {
        DataTable dt = DAL.Tables.T_Banks.Select("", "", "[id]");

        if (dt == null)
        {
            PublicFunction.GoError(ErrorNumber.DataReadWrite, "数据读写错误", this.GetType().BaseType.FullName);

            return;
        }

        ShoveEL.ControlExt.FillDropDownList(ddlBankName, dt, "Name", "ID");
    }

    protected void ddlUserProvince_SelectedIndexChanged(object sender, System.EventArgs e)
    {
        FillCity(int.Parse(ddlUserProvince.SelectedValue));
    }

    private void BindData(long id)
    {
        User tu = new User();
        tu.ID = id;
        string Return = "";
        int Result = tu.GetUserInfoByID(ref Return);
        if (Result != 0)
        {
            PublicFunction.GoError(ErrorNumber.NotEnoughCompetence, "对不起，您没有足够的权限访问此页面", this.GetType().BaseType.FullName);

            return;
        }

        BindData(tu);
    }

    private void BindData(User tu)
    {
        DataTable dt = DAL.Tables.T_City.Select("ProvinceID", "[id] = " + tu.CityID.ToString(), "[id]");

        if (dt == null)
        {
            PublicFunction.GoError(ErrorNumber.DataReadWrite, "数据读写错误", this.GetType().BaseType.FullName);

            return;
        }

        long ProvinceID = -1;
        try
        {
            ProvinceID = ShoveEL._Convert.StrToLong(dt.Rows[0][0].ToString(), -1);
        }
        catch { }

        if (ProvinceID < 0)
        {
            ProvinceID = 1;
            tu.CityID = 1;
        }

        ShoveEL.ControlExt.SetDownListBoxTextFromValue(ddlUserProvince, ProvinceID.ToString());
        FillCity(int.Parse(ddlUserProvince.SelectedValue));
        ShoveEL.ControlExt.SetDownListBoxTextFromValue(ddlUserCity, tu.CityID.ToString());

        ShoveEL.ControlExt.SetDownListBoxTextFromValue(ddlBankName, tu.BankTypeID.ToString());

        tbUserName.Text = tu.Name;
        tbUserRealityName.Text = tu.RealityName;
        tbUserIDCardNumber.Text = tu.IDCardNumber;
        //tbUserPassword.Text = tu.Password;
        //tbUserPassword2.Text = tu.Password2;
        tbUserEmail.Text = tu.Email;

        tbUserTelephone.Text = tu.Telephone;
        tbUserMobile.Text = tu.Mobile;
        tbUserQQ.Text = tu.QQ;
        tbUserAddress.Text = tu.Address;
        if (tu.Sex == "男")
        {
            rbUserSexM.Checked = true;
        }
        else if (tu.Sex == "女")
        {
            rbUserSexW.Checked = true;
        }
        dtUserBirthday.Value = tu.Birthday;

        ShoveEL.ControlExt.SetDownListBoxTextFromValue(ddlBankName, tu.BankTypeName);
        tbUserBankNameDetail.Text = tu.BankNameDetail;
        tbUserBankCardNumber.Text = tu.BankCardNumber;
        cbSurrogate.Checked = tu.isSurrogate;

        labUserType.Text = "";
        for (int i = 0; i < tu.UserType; i++)
        {
            labUserType.Text += "<img src='../Images/gold.gif' border='0'>";
        }

        //ddlUserType.SelectedValue = tu.UserType.ToString();

        tbID.Text = tu.ID.ToString();

        btnSave.Enabled = true;
        btnSave.Visible = true;

        btnNetEdit.Enabled = false;
        btnNetEdit.Visible = false;

        btnUserNet.Enabled = true;
        btnUserNet.Visible = true;
        btnUserScore.Enabled = true;
        btnUserScore.Visible = true;
        btnUserScore2.Enabled = true;
        btnUserScore2.Visible = true;

        BindDataForScore(tu);
    }

    private void BindDataForScore(User tu)
    {
		long L = 0;
		long C = 0;
		long R = 0;
		long L_CurrentMonth = 0;
		long C_CurrentMonth = 0;
		long R_CurrentMonth = 0;
		long L_CurrentWeek = 0;
		long C_CurrentWeek = 0;
		long R_CurrentWeek = 0;
		long L_CurrentDay = 0;
		long C_CurrentDay = 0;
		long R_CurrentDay = 0;

        //DataSet ds = DAL.Procedures.P_GetScoreForUser.Open(tu.ID, ref L, ref C, ref R, ref L_CurrentMonth, ref C_CurrentMonth, ref R_CurrentMonth, ref L_CurrentWeek, ref C_CurrentWeek, ref R_CurrentWeek, ref L_CurrentDay, ref C_CurrentDay, ref R_CurrentDay);

        //if (ds == null)
        //{
        //    ShoveEL._Web.JavaScript.Alert(this, "数据库连接失败");

        //    return;
        //}

        labSLM.Text = L_CurrentMonth.ToString();
        labSCM.Text = C_CurrentMonth.ToString();
        labSRM.Text = R_CurrentMonth.ToString();

        labSLW.Text = L_CurrentWeek.ToString();
        labSCW.Text = C_CurrentWeek.ToString();
        labSRW.Text = R_CurrentWeek.ToString();

        labSL.Text = L_CurrentDay.ToString();
        labSC.Text = C_CurrentDay.ToString();
        labSR.Text = R_CurrentDay.ToString();


        User tu_2 = new User();
        string Return = "";

        if (tu.CommendID > 0)
        {
            tu_2.ID = tu.CommendID;
            if (tu_2.GetUserInfoByID(ref Return) == 0)
            {
                labCommend.Text = tu_2.Name + "," + tu_2.RealityName;
            }
        }

        if (tu.ParentID > 0)
        {
            tu_2.ID = tu.ParentID;
            if (tu_2.GetUserInfoByID(ref Return) == 0)
            {
                labParent.Text = tu_2.Name + "," + tu_2.RealityName;
            }

            if (tu.LR == 1)
            {
                labLR.Text = "左区";
            }
            if (tu.LR == 2)
            {
                labLR.Text = "中区";
            }
            if (tu.LR == 3)
            {
                labLR.Text = "右区";
            }
        }
    }

    protected void btnRead_Click(object sender, System.EventArgs e)
    {
        if (tbUserName.Text.Trim() == "")
        {
            ShoveEL._Web.JavaScript.Alert(this.Page, "请输入用户名。");

            return;
        }

        User tu = new User();
        tu.Name = tbUserName.Text.Trim();

        string Return = "";
        int Result = tu.GetUserInfoByName(ref Return);
        if (Result != 0)
        {
            btnSave.Enabled = false;
            btnSave.Visible = false;
            btnNetEdit.Enabled = false;
            btnNetEdit.Visible = false;
            btnUserNet.Enabled = false;
            btnUserNet.Visible = false;

            ShoveEL._Web.JavaScript.Alert(this.Page, Return);
            return;
        }

        labSLM.Text = "";
        labSCM.Text = "";
        labSRM.Text = "";

        labSLW.Text = "";
        labSCW.Text = "";
        labSRW.Text = "";

        labSL.Text = "";
        labSC.Text = "";
        labSR.Text = "";
        labCommend.Text = "";
        labParent.Text = "";
        labLR.Text = "";

        BindData(tu);
    }

    protected void btnSave_Click(object sender, System.EventArgs e)
    {
        if (tbID.Text == "")
        {
            PublicFunction.GoError(ErrorNumber.NotEnoughCompetence, "对不起，您没有足够的权限访问此页面", this.GetType().BaseType.FullName);
            return;
        }

        User tu = new User();
        tu.ID = int.Parse(tbID.Text);

        string Return = "";
        int Result = tu.GetUserInfoByID(ref Return);
        if (Result != 0)
        {
            ShoveEL._Web.JavaScript.Alert(this.Page, Return);

            return;
        }
        if (tu.Name != tbUserName.Text.Trim())
        {
            ShoveEL._Web.JavaScript.Alert(this.Page, "不能修改用户名。");

            return;
        }

        if (tbUserRealityName.Text.Trim() == "")
        {
            ShoveEL._Web.JavaScript.Alert(this.Page, "请输入真实姓名。");

            return;
        }

        if (cbUpdatePassword.Checked)
        {
            if (tbUserPassword.Text == "")
            {
                ShoveEL._Web.JavaScript.Alert(this.Page, "请输入用户密码。");

                return;
            }
        }

        if (cbUpdatePassword2.Checked)
        {
            if (tbUserPassword2.Text == "")
            {
                ShoveEL._Web.JavaScript.Alert(this.Page, "请输入用户二级密码。");

                return;
            }
        }

        if (tbUserMobile.Text.Trim() == "")
        {
            ShoveEL._Web.JavaScript.Alert(this.Page, "请输入手机号码。");

            return;
        }

        if (tbUserBankNameDetail.Text.Trim() == "")
        {
            ShoveEL._Web.JavaScript.Alert(this.Page, "请输入详细开户银行名称。");

            return;
        }

        if (tbUserBankCardNumber.Text.Trim() == "")
        {
            ShoveEL._Web.JavaScript.Alert(this.Page, "请输入银行卡号。");

            return;
        }

        tu.RealityName = ShoveEL._Convert.ToDBC(tbUserRealityName.Text.Trim());
        tu.Sex = rbUserSexM.Checked ? "男" : (rbUserSexW.Checked ? "女" : "");
        tu.Birthday = dtUserBirthday.Value;
        //tu.UserType = ShoveEL._Convert.StrToInt(ddlUserType.SelectedValue, 0);

        if (cbUpdatePassword.Checked)
        {
            tu.Password = PublicFunction.EncryptPassword(tbUserPassword.Text.Trim());
        }

        if (cbUpdatePassword2.Checked)
        {
            tu.Password2 = PublicFunction.EncryptPassword(tbUserPassword2.Text.Trim());
        }

        tu.Email = ShoveEL._Convert.ToDBC(tbUserEmail.Text.Trim());
        tu.CityID = int.Parse(ddlUserCity.SelectedValue);

        tu.IDCardNumber = ShoveEL._Convert.ToDBC(tbUserIDCardNumber.Text.Trim());
        tu.Telephone = ShoveEL._Convert.ToDBC(tbUserTelephone.Text.Trim());
        tu.Mobile = ShoveEL._Convert.ToDBC(tbUserMobile.Text.Trim());
        tu.QQ = ShoveEL._Convert.ToDBC(tbUserQQ.Text.Trim());
        tu.Address = ShoveEL._Convert.ToDBC(tbUserAddress.Text.Trim());
        //tu.BankTypeName = ddlBankName.SelectedValue;
        tu.BankTypeID = ShoveEL._Convert.StrToShort(ddlBankName.SelectedValue, 1);
        tu.BankNameDetail = tbUserBankNameDetail.Text.Trim();
        tu.BankCardNumber = ShoveEL._Convert.ToDBC(tbUserBankCardNumber.Text.Trim());

        tu.isSurrogate = cbSurrogate.Checked;        

        Return = "";
        if (tu.EditByID(ref Return) != 0)
        {
            ShoveEL._Web.JavaScript.Alert(this.Page, Return);

            return;
        }

        if (tu.ID == _User.ID)
        {
            _User.GetUserInfoByID(ref Return);
        }

        //写入操作日志
        OperationLog Log = new OperationLog();

        Log._Memo = _User.Name + "修改" + tu.Name + "用户资料信息";
        Log._OpDateTime = DateTime.Now;
        Log._UserID = _User.ID;
        Log._UserIP = System.Web.HttpContext.Current.Request.UserHostAddress;

        Log.Add();
        //操作日志写入完成

        ShoveEL._Web.JavaScript.Alert(this.Page, "用户资料已经保存成功。");
    }

    protected void btnNetEdit_Click(object sender, System.EventArgs e)
    {
        if (tbID.Text == "")
        {
            PublicFunction.GoError(ErrorNumber.NotEnoughCompetence, "对不起，您没有足够的权限访问此页面", this.GetType().BaseType.FullName);
            return;
        }

        this.Response.Redirect("AdminUserNetEdit.aspx?id=" + tbID.Text);
    }

    protected void btnUserNet_Click(object sender, System.EventArgs e)
    {
        if (tbID.Text == "")
        {
            PublicFunction.GoError(ErrorNumber.NotEnoughCompetence, "对不起，您没有足够的权限访问此页面", this.GetType().BaseType.FullName);
            return;
        }

        this.Response.Redirect("NetCopy.aspx?id=" + tbID.Text);
    }

    protected void btnUserScore_Click(object sender, System.EventArgs e)
    {
        if (tbID.Text == "")
        {
            PublicFunction.GoError(ErrorNumber.NotEnoughCompetence, "对不起，您没有足够的权限访问此页面", this.GetType().BaseType.FullName);
            return;
        }

        this.Response.Redirect("Score2.aspx?id=" + tbID.Text);
    }

    protected void btnUserScore2_Click(object sender, System.EventArgs e)
    {
        if (tbID.Text == "")
        {
            PublicFunction.GoError(ErrorNumber.NotEnoughCompetence, "对不起，您没有足够的权限访问此页面", this.GetType().BaseType.FullName);
            return;
        }

        this.Response.Redirect("Score.aspx?id=" + tbID.Text);
    }
}
